package com.wu.crud.service;

import com.wu.crud.bean.Admin;
import com.wu.crud.bean.AdminExample;
import com.wu.crud.bean.AdminExample.Criteria;
import com.wu.crud.dao.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Autowired
    AdminMapper adminMapper;

    /**
     * 查询所有管理员
     * @return
     */
    public List<Admin> getAll(){
        return adminMapper.selectByExample(null);
    }

    /**
     * 添加管理员
     * @param admin
     */
    public void insertAdmin(Admin admin){
        adminMapper.insertSelective(admin);
    }

    /**
     * 检验名字是否可用
     * @param adminName
     * @return
     */
    public boolean checkAdmin(String adminName) {
        // TODO Auto-generated method stub
        AdminExample example = new AdminExample();
        Criteria criteria = example.createCriteria();
        criteria.andAnameEqualTo(adminName);
        long count = adminMapper.countByExample(example);
        return count == 0;
    }

    /**
     * 按照管理员aid查询管理员
     * @param aid
     * @return
     */
    public Admin getAdmin(String aid){
        return adminMapper.selectByPrimaryKey(aid);
    }

    /**
     * 管理员更新
     * @param admin
     */
    public void updateAdmin(Admin admin){
        adminMapper.updateByPrimaryKey(admin);
    }

    /**
     * 管理员删除
     * @param aid
     */
    public void deleteAdmin(String aid){
        adminMapper.deleteByPrimaryKey(aid);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<String> ids){
        AdminExample adminExample = new AdminExample();
        Criteria criteria = adminExample.createCriteria();
        criteria.andAidIn(ids);
        adminMapper.deleteByExample(adminExample);
    }
}
